package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.GsonUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.CommonPhotoAdapter;
import flc.ast.bean.IdPhotosBean;
import flc.ast.databinding.ActivityCommonPhotoBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.IOUtil;

public class CommonPhotoActivity extends BaseAc<ActivityCommonPhotoBinding> {

    private CommonPhotoAdapter commonPhotoAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_common_photo;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivCommonPhotoBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvIdPhotoList.setLayoutManager(new LinearLayoutManager(mContext));
        commonPhotoAdapter = new CommonPhotoAdapter();
        mDataBinding.rvIdPhotoList.setAdapter(commonPhotoAdapter);
        commonPhotoAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        Content.sIdPhotosBean = commonPhotoAdapter.getItem(position);
        startActivity(PhotoDetailActivity.class);
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        try {
            InputStream is = mContext.getAssets().open("idPhoto0.json");
            String json = IOUtil.readStream2Str(is);
            List<IdPhotosBean> idPhotosBeans = GsonUtils.fromJson(json, new TypeToken<List<IdPhotosBean>>() {
            }.getType());
            commonPhotoAdapter.setList(idPhotosBeans);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}